package com.project.livetv.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.project.livetv.callback.NetCallBack;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by li on 2016/5/9.
 */
public class BaseFragment extends Fragment{

    private Callback.Cancelable cancelable;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    /**
     * 下载数据
     */
    protected void downLoadData(String url, final NetCallBack netCallBack){
        RequestParams params = new RequestParams(url);
        if(cancelable!=null&&cancelable.isCancelled()){
            cancelable.cancel();
        }
        netCallBack.onStart();
        cancelable = x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                netCallBack.onSucess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                netCallBack.onError(ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

                netCallBack.onCancled();
            }

            @Override
            public void onFinished() {
                netCallBack.onFinished();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(cancelable!=null&&cancelable.isCancelled()){
            cancelable.cancel();
        }
    }
}
